package com.client.koshercook.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;
/** popular recipes */
public class TopRated extends Composite{


	private VerticalPanel VPanel = new VerticalPanel();
	//private HorizontalPanelRoundEdges edgesPanel = new HorizontalPanelRoundEdges(VPanel, 160);
	private EdgesPanel edgesPanel = new EdgesPanel(VPanel);
	private FlexTable table = new FlexTable();
	private HTML header = new HTML("מתכונים פופולרים באתר");
	
	public TopRated() {
		
		VPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		VPanel.add(header);
		VPanel.add(table);
		VPanel.setStyleName("midPanel");
		table.setStyleName("topRatedTable");
		
		initWidget(edgesPanel);
	}

	
	private Button getLink(final String link, final String recipeName){
		
		Button res = new Button(recipeName);
		res.setStyleName("empty");
		res.setWidth("8em");
		
		res.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				Client.model.setRecipeInfoPanelRecipe(link, recipeName);
			}
		});
		
		return res;
	}
	
	/** build the recipes list */
	public void fillTable(){
		List<String> topViewed = new ArrayList<String>();	
		topViewed = Client.model.getTopViewed();
		
		//fill the table
		int i = 0;
		for (String s : topViewed) {
			int j = s.indexOf("@");
			String link = s.substring(j + 1);
			String recName = s.substring(0, j);
			
			table.setWidget(i, 0, getLink(link, recName));
			table.setHTML(i, 1, "."+ Integer.toString(i+1) );

			i++;
		}
	}
}
